#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

FILE *open_file(const char *fpath, const char *mode)
{
    FILE *f = fopen(fpath, mode);
    if(!f)
    {
        perror(NULL);
        fprintf(stderr, "ERROR: could not open file: ");
        exit(EXIT_FAILURE);
    }
    return f;
}

int main(void)
{
    errno = 0;
    FILE *f = open_file("/home/amf/.dotfiles/MANIFEST", "r");
    char *buffer = NULL;
    size_t bufsize = 0;
    while(1){
        int n_read = getline(&buffer, &bufsize, f);
        char *dir = buffer;
        char *symlink = strtok(dir, ":");
        printf("%s -> %s\n", dir, symlink);
        if(n_read < 0) break;
    }

    if(errno)
    {
        perror(NULL);
        fprintf(stderr, "ERROR: at %s: %s, line %d\n", __FILE__, __FUNCTION__, __LINE__);
        if(buffer)
        {
            free(buffer);
            exit(EXIT_FAILURE);
        }
    }
    printf("%s\n", buffer);
    fclose(f);
    free(buffer);                           //DO NOT FORGET
    return 0;
}
